package com.mright.service.impl;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mright.dao.DaoSupport;
import com.mright.entity.SysPermission;
import com.mright.entity.SysRole;
import com.mright.entity.SysUser;
import com.mright.model.LoginModel;
import com.mright.model.PageData;
import com.mright.service.AccountService;

@Service
public class AccountServiceImpl implements AccountService
{
	@Autowired
	private DaoSupport dao;

    @Override
    public int addUser(SysUser user) throws Exception {
    	dao.save("UserMapper.addUser",user);
        return user.getId();
    }

    @Override
    public boolean login(LoginModel loginModel)throws Exception {
		SysUser user =  (SysUser) dao.findForObject("UserMapper.loginByModel",loginModel);
		if(null!=user) return true;
    	return false;
    }

	@Override
	public SysUser findUserByNameAndPwd(PageData pd) throws Exception {
		// TODO Auto-generated method stub
		return  (SysUser) dao.findForObject("UserMapper.findUserByNameAndPwd",pd);
	}
	
	@Override
	public SysUser findByUserName(String userName) throws Exception {
		return (SysUser) dao.findForObject("UserMapper.findByUserName", userName);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SysRole> getRolesByUserId(int userId) throws Exception{
		return (List<SysRole>)dao.findForList("RoleMapper.getRolesByUserId", userId);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public  List<SysPermission> getPermissionsByUserId(int userId) throws Exception {
		return (List<SysPermission>)dao.findForList("PermissionMapper.getPermissionsByUserId", userId);
	}
	
	@Override
	public void updateLastLogin(PageData pd) throws Exception {
		// TODO Auto-generated method stub
		
	}
    
    
}
